#include "QReflection.h"
#include <QPainter>
#include <QPixmap>
#include <QPalette>
#include <QImage>


static QImage *mirrorImage (const QImage *image) {
	QImage *tmpImage = new QImage(image->mirrored(false, true));

	QPoint p1, p2;
	p2.setY(tmpImage->height());
	
	QLinearGradient gradient(p1, p2);
	gradient.setColorAt(0, Qt::transparent);
	gradient.setColorAt(1, QColor(0, 0, 0, 255));

	QPainter p(tmpImage);
	p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
	p.fillRect(0, 0, tmpImage->width(), tmpImage->height(), gradient);

	gradient.setColorAt(0,QColor(0, 0, 0, 255));
	gradient.setColorAt(1, Qt::transparent);
	p.fillRect(0,0, tmpImage->width(), tmpImage->height(), gradient);

	p.end();

	return(tmpImage);
}

QReflection::QReflection( QWidget * parent, Qt::WindowFlags f ):QWidget(parent,f)
{
	  QPalette palette;
    QBrush brush(QColor(255, 255, 255, 255));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Base, brush);
    QBrush brush1(QColor(0, 0, 0, 255));
    brush1.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Window, brush1);
    palette.setBrush(QPalette::Inactive, QPalette::Base, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Window, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::Base, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::Window, brush1);
    this->setPalette(palette);
}

QReflection::~QReflection(void)
{
}

void QReflection::paintEvent ( QPaintEvent * event )
{
	QPainter p(this);
	QPixmap pix("bugs_bunny.jpg");



	QRectF source(0.0, 0.0, pix.width(), pix.height());
	QRectF target((this->geometry().width()/2)-(source.width()/2), (this->geometry().height()/2)-(source.height()/2), 400.0, 200.0);
	
	p.drawPixmap(target,pix,source);

	QRectF source1(0.0, 0.0, pix.width(), pix.height());
	QRectF target1((this->geometry().width()/2)-(source1.width()/2), (this->geometry().height()/2)-(source1.height()/2)+target.height(), 400.0, 200.0);

	QImage *img = new QImage(pix.toImage());
	
	img = mirrorImage(img);

	// Draw Image Reflection
	
	p.setCompositionMode(QPainter::CompositionMode_Source);
	p.drawPixmap(target1, QPixmap::fromImage(*img),source1);
}
